package com.foreknow.springboot_demo.threaddemo;

public class Ticket extends Thread{

    private static  int num = 100;

    @Override
   public void run() {
        while (true){
            //synchronized线程同步：当某个线程正在执行的时候，
            //其他线程有可能会修改票数，所以要使用synchronized
            //加锁，当前线程我在执行的时候，其他线程无法修改票数
            synchronized (Ticket.class){
                num--;
                if (num <= 0){
                    break;
                }
                try {
                    Thread.sleep(60);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(getName()+" 售出这是第" + num +"号");
            }
        }
    }


    public static void main(String[] args) {
        Ticket t1 = new Ticket();
        Ticket t2 = new Ticket();
        Ticket t3 = new Ticket();
        Ticket t4 = new Ticket();
        t1.setName("窗口1");
        t2.setName("窗口2");
        t3.setName("窗口3");
        t4.setName("窗口4");
        t1.start();
        t2.start();
        t3.start();
        t4.start();
    }
}
